package com.company.project.robot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.robot.entity.SendRecordEntity;
import com.company.project.robot.service.RobotService;
import com.company.project.robot.service.SendRecordService;
import com.company.project.robot.service.WechatGroupService;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.ArrayList;
import java.util.List;

import com.company.project.common.utils.DataResult;


/**
 * 发送记录
 *
 * @author zyf
 * @email 2102378781@qq.com
 * @date 2021-11-17 16:20:53
 */
@Controller
@RequestMapping("/")
public class SendRecordController {
    @Autowired
    private SendRecordService sendRecordService;

    @Autowired
    private RobotService robotService;

    @Autowired
    private WechatGroupService wechatGroupService;


    /**
     * 跳转到页面
     */
    @GetMapping("/index/sendRecord")
    public String sendRecordList() {
        return "sendrecord/list";
    }

    /**
     * 跳转到修改页面
     */
    @GetMapping("/index/sendRecord/info")
    public String sendRecordInfo(String id, Model model) {
        model.addAttribute("id", id);
        return "sendrecord/edit";
    }

    @GetMapping("/index/selectWechat")
    public String selectWechat(String id, Model model) {
        model.addAttribute("id", id);
        return "sendrecord/selectWechat";
    }

    @ApiOperation(value = "新增")
    @PostMapping("sendRecord/add")
    @ResponseBody
    public DataResult add(@RequestBody SendRecordEntity sendRecord) {
        sendRecordService.saveSendFile(sendRecord);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("sendRecord/delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        sendRecordService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("sendRecord/update")
    @ResponseBody
    public DataResult update(@RequestBody SendRecordEntity sendRecord) {
        sendRecordService.updateById(sendRecord);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("sendRecord/listByPage")
    @ResponseBody
    public DataResult findListByPage(@RequestBody SendRecordEntity sendRecord) {
        Page page = new Page(sendRecord.getPage(), sendRecord.getLimit());
        LambdaQueryWrapper<SendRecordEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(SendRecordEntity::getId, sendRecord.getId());
        IPage<SendRecordEntity> iPage = sendRecordService.page(page, queryWrapper);

        for (SendRecordEntity recordEntity : iPage.getRecords()) {
            List<String> wechatGroundName = new ArrayList<>();
            String[] newArr = new String[0];
            recordEntity.setRobotName(robotService.getById(recordEntity.getRobotId()).getName());
            String[] wechatGrounds = recordEntity.getWechatGroupId().split(",");
            for (String wechatGroundIs : wechatGrounds) {
                wechatGroundName.add(wechatGroupService.getById(wechatGroundIs).getGroupName());
                newArr = wechatGroundName.toArray(new String[wechatGroundName.size()]);
            }
            recordEntity.setWechatGroupName(ArrayUtils.toString(newArr, ","));
        }

        return DataResult.success(iPage);
    }

    @GetMapping("/sendRecord/{id}")
    @ApiOperation(value = "查询详情接口")
    @ResponseBody
    public DataResult detailInfo(@PathVariable("id") String id) {
        SendRecordEntity recordEntity =  sendRecordService.getById(id);

        List<String> wechatGroundName = new ArrayList<>();
        String[] newArr = new String[0];
        recordEntity.setRobotName(robotService.getById(recordEntity.getRobotId()).getName());
        String[] wechatGrounds = recordEntity.getWechatGroupId().split(",");
        for (String wechatGroundIs : wechatGrounds) {
            wechatGroundName.add(wechatGroupService.getById(wechatGroundIs).getGroupName());
            newArr = wechatGroundName.toArray(new String[wechatGroundName.size()]);
        }
        recordEntity.setWechatGroupName(ArrayUtils.toString(newArr, ","));
        return DataResult.success(recordEntity);
    }

}
